package com.atguigu.gulimall.cart.controller;

import com.atguigu.common.utils.R;
import com.atguigu.gulimall.cart.service.CartService;
import com.atguigu.gulimall.cart.vo.Cart;
import com.atguigu.gulimall.cart.vo.CartItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.ExecutionException;

@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    @GetMapping("/getUserCartItems")
    public List<CartItem> getUserCartItems(){
        return cartService.getUserCartItems();
    }

    /**
     * 购物车列表
     * @return
     */
    @GetMapping("/index")
    public R cartList(){
        Cart cart = cartService.getCart();
        return R.ok().setData(cart);
    }

    /**
     * 添加到购物车
     * @return
     */
    @PostMapping("/addToCart")
    public R addToCart(@RequestParam("skuId") Long skuId,@RequestParam("num") Integer num) throws ExecutionException, InterruptedException {
        Cart cart = cartService.addToCart(skuId, num);
        return R.ok().setData(cart);
    }

    /**
     * 选择购物项
     * @param skuId
     * @param check
     * @return
     */
    @PostMapping("/checkItem")
    public R checkItem(@RequestParam("skuId") Long skuId,@RequestParam("check") Integer check){
        Cart cart = cartService.checkItem(skuId, check);
        return R.ok().setData(cart);
    }

    /**
     * 修改购物车数量
     * @param skuId
     * @param count
     * @return
     */
    @PostMapping("/countItem")
    public R countItem(@RequestParam("skuId") Long skuId,@RequestParam("count") Integer count){
        Cart cart = cartService.countItem(skuId,count);
        return R.ok().setData(cart);
    }

    /**
     * 删除购物项
     * @param skuId
     * @return
     */
    @PostMapping("/deleteItem")
    public R deleteItem(@RequestParam("skuId") Long skuId){
        Cart cart = cartService.deleteItem(skuId);
        return R.ok().setData(cart);
    }
}
